package net.getbang.code.service.impl;

import java.io.ByteArrayOutputStream;
import java.util.List;
import java.util.Map;
import java.util.zip.ZipOutputStream;

import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import net.getbang.code.mapper.SysGeneratorMapper;
import net.getbang.code.service.SysGeneratorService;
import net.getbang.code.utils.GeneratorUtils;
@Service
public class SysGeneratorServiceImpl implements SysGeneratorService {

	@Autowired
	private SysGeneratorMapper sysGeneratorMapper;
	
	@Override
	public List<Map<String, Object>> queryList(Map<String, Object> map) {
		// TODO Auto-generated method stub
		 return sysGeneratorMapper.queryList(map);
	}

	@Override
	public int queryTotal(Map<String, Object> map) {
		
		 return sysGeneratorMapper.queryTotal(map);
	}

	@Override
	public Map<String, String> queryTable(String tableName) {
		// TODO Auto-generated method stub
		 return sysGeneratorMapper.queryTable(tableName);
	}

	@Override
	public List<Map<String, String>> queryColumns(String tableName) {
		// TODO Auto-generated method stub
		 return sysGeneratorMapper.queryColumns(tableName);
	}

	@Override
	public byte[] generatorCode(String[] tableNames) {
		// TODO Auto-generated method stub
		 ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
		 
		 ZipOutputStream zip = new ZipOutputStream(outputStream);
		 

	        for(String tableName : tableNames){
	            //查询表信息
	            Map<String, String> table = queryTable(tableName);
	            //查询列信息
	            List<Map<String, String>> columns = queryColumns(tableName);
	            //生成代码
	            GeneratorUtils.generatorCode(table, columns, zip);
	        }
		 
	        IOUtils.closeQuietly(zip);
	        return outputStream.toByteArray();
	}

}
